<template>
  <div class="page_content">
    <page-header/>
    <el-menu
      :default-active="'-1'"
      class="el-menu-demo"
      mode="horizontal"
      background-color="#545c64"
      text-color="#fff"
      active-text-color="#ffd04b">
      <el-menu-item :index="index+''" v-for="(item,index) in homeClassify" v-bind:key="index">
        <el-link :href="'/?i='+index">{{item.channel_name}}</el-link>
      </el-menu-item>
    </el-menu>

    <div>
      <el-row>
        <el-col :span="8" v-for="index in list.length" v-bind:key="index">
          <el-link :href="list[index-1].detailurl">
            <el-image :src="list[index-1].coverurl" fit="cover" class="cover"/>
            <div class="name">{{list[index-1].title}}</div>
          </el-link>
        </el-col>
      </el-row>
    </div>

    <div class="load_more" @click="load" v-if="!loading">点击加载更多内容</div>
    <div v-if="noMore" class="load_desc">全部加载完成~~</div>
    <div v-if="loading" class="load_desc">正在加载中</div>

  </div>
</template>

<script>
  import axios from "axios";

  function getQueryVariable(variable) {
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i = 0; i < vars.length; i++) {
      var pair = vars[i].split("=");
      if (pair[0] == variable) {
        return pair[1];
      }
    }
    return (false);
  }

  var currentPage = 2;

  export default {
    name: "search",
    head:{
      title:"视频站搜索页"
    },
    data() {
      return {
        noMore: false,
        loading: false
      }
    },
    methods: {
      load() {
        this.loading = true;
        var that = this;
        var key = getQueryVariable("key");
        axios.get('/down/daHai/search?key=' + key + "&page=" + currentPage).then(function (response) {
          that.loading = false;
          console.log(response.data.data.page.hasNext);
          if (!response.data.data.page.hasNext) {
            that.noMore = true;
            console.log("已经加载结束了");
          }
          currentPage++;
          for (let i = 0; i < response.data.data.data.length; i++) {
            that.list.push(response.data.data.data[i]);
          }
        }).catch(function (error) {
          alert(error);
          that.loading = false;
        });
      }
    },
    async asyncData({query}) {
      console.log(query.key);
      let homeClassify = await axios.get('http://47.117.121.53:8080/down/daHai/homeClassify');
      let content = await axios.get('http://47.117.121.53:8080/down/daHai/search?key=' + encodeURI(query.key));
      console.log(content.data.data.data);
      return {
        homeClassify: homeClassify.data.data.result,
        list: content.data.data.data,
      }
    }
  }
</script>

<style lang="scss">
  @import "assets/Common";

  .el-link {
    width: 100%;
    display: block;
  }

  .infinite-list-wrapper {
    height: 600px;
  }

  .el-image {
    width: 100%;
    height: 400px;
  }

  .el-row {
    .el-col {
      margin-top: 10px;
    }

    .el-col {
      padding-left: 10px;
    }
  }

  .name {
    margin-top: 10px;
    color: #333333;
    font-size: 18px;
  }

  .load_more{
    width: $--window-width;
    background-color: #f2f2f2;
    cursor: pointer;
    line-height: 40px;
    text-align: center;
    margin-top: 10px;
  }

  .load_desc{
    line-height: 40px;
    text-align: center;
    margin-top: 10px;
  }
</style>
